﻿/*
 * @author: wizardc
 */

namespace Dou.Utils
{
    /// <summary>
    /// 位运算工具类
    /// </summary>
    public static class BitUtil
    {
        /// <summary>
        /// 设置指定位的值
        /// </summary>
        public static uint SetBit(uint num, int pos, bool value)
        {
            if (value)
            {
                num = (uint) (num | (1 << pos));
            }
            else
            {
                num = (uint) (num & (~(1 << pos)));
            }
            return num;
        }

        /// <summary>
        /// 获取指定位的值
        /// </summary>
        public static bool GetBit(uint num, int pos)
        {
            return num == (num | (1 << pos));
        }

        /// <summary>
        /// 交换指定位的值
        /// </summary>
        public static uint SwitchBit(uint num, int pos)
        {
            num = (uint) (num ^ (1 << pos));
            return num;
        }
    }
}
